﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain.Editor;
using Brain.SunBurn;
using SynapseGaming.LightingSystem.Rendering;
using Microsoft.Xna.Framework.Graphics;

namespace Brain.Plugins.SunBurn
{
    public class SunBurnPluginForward : IEditorPlugin
    {
        public void Load()
        {
            SplashScreenGameComponent comp = new SplashScreenGameComponent(Engine.Instance.Game);
            Engine.Instance.Game.Components.Add(comp);

            Engine.Instance.AddManager(new SunBurnRenderManager3D(Engine.Instance.GraphicsDevice, RenderingSystemType.Forward));

            GameScreen s = Engine.Instance.ScreenManager.GetScreen(0);
            // This GameScreen "s" SHOULD NEVER BE NULL
            // We are only checking if it's null because you know,
            // some plugins 'might' just end-up destroying the system... IDK
            if (s != null)
            {
                InputFreeCamera3D free = new InputFreeCamera3D(s);
                free.ShouldSerialize = false;

                free.FarPlane = 10000;
                free.NearPlane = 1;
                PresentationParameters pp = Engine.Instance.GraphicsDevice.PresentationParameters;
                free.AspectRatio = pp.BackBufferWidth / (float)pp.BackBufferHeight;

                Engine.Instance.CameraManager.Camera3D = free;
            }
        }


        public void Reload()
        {
            Engine.Instance.AddManager(new SunBurnRenderManager3D(Engine.Instance.GraphicsDevice, RenderingSystemType.Forward));

            // Reload is called 'usually' when the Engine is loading a Scene from a file.
            // This is the very first method to be called, so we don't have any GameScreens active.
            // The Camera we did on the Load project must wait to be done on the EndReload()
        }

        public void EndReload()
        {
            GameScreen s = Engine.Instance.ScreenManager.GetScreen(0);
            if (s != null)
            {
                InputFreeCamera3D free = new InputFreeCamera3D(s);
                free.ShouldSerialize = false;

                free.FarPlane = 10000;
                free.NearPlane = 1;
                PresentationParameters pp = Engine.Instance.GraphicsDevice.PresentationParameters;
                free.AspectRatio = pp.BackBufferWidth / (float)pp.BackBufferHeight;

                Engine.Instance.CameraManager.Camera3D = free;
            }
        }
    }
}
